<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 这是头像区域 -->
      <div class="avatar_box">
        <img src="../assets/猫咪.png" alt="" />
      </div>
      <el-tabs
        class="activeTab"
        v-model="activeName"
        @tab-click="changeFunction"
      >
        <el-tab-pane label="登录" name="first"></el-tab-pane>
        <el-tab-pane label="注册" name="second"></el-tab-pane>
      </el-tabs>
      <!-- 这是登录表单区域 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 登录邮箱 -->
        <el-form-item prop="email">
          <el-input
            placeholder="邮箱登录"
            v-model="loginForm.email"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="loginForm.password"
            prefix-icon="el-icon-key"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 选择用户类型 -->
        <el-form-item v-show="activeFunction == false">
          <el-radio-group v-model="loginTypename" @change="changeType">
            <el-radio label="候选人"></el-radio>
            <el-radio label="面试官"></el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button
            v-show="activeFunction == true"
            type="primary"
            @click="login"
            >登录</el-button
          >
          <el-button
            v-show="activeFunction == false"
            tyoe="success"
            @click="newlogin"
            >注册</el-button
          >
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var checkEmail = (rule, value, cb) => {
      //验证邮箱的正则表达式
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
      if (regEmail.test(value)) {
        //合法的邮箱
        return cb();
      }
      //邮箱不合法
      cb(new Error("请输入合法的邮箱！"));
    };
    return {
      activeName: "first",
      activeFunction: true,
      loginTypename: "",
      // 登录表单数据绑定对象
      loginForm: {
        email: "",
        password: "",
        type: 1,
      },
      // 登陆表单的验证规则对象
      loginFormRules: {
        // 验证用户邮箱
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { validator: checkEmail, trigger: "blur" },
        ],
        // 验证密码
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    //
    changeFunction() {
      this.activeFunction = !this.activeFunction;
      console.log(this.activeFunction);
    },
    // 选择用户类型
    changeType(histtype) {
      if (histtype == "候选人") this.loginForm.type = 1;
      else this.loginForm.type = 2;
    },

    //点击重置按钮重置表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    // 注册
    newlogin() {
      this.$refs.loginFormRef.validate((valid) => {
        if (!valid) return;
        const InspireCloud = require("@byteinspire/js-sdk");
        const serviceId = "qcqsjd"; // 替换成你的 serviceId，可在后台「设置」页面获取

        // 初始化
        const inspirecloud = new InspireCloud({ serviceId });

        // 函数名称
        const fnName = "user_create";
        console.log({
          userEmail: this.loginForm.email,
          userPassword: this.loginForm.password,
          userType: this.loginForm.type,
        });
        // 通过 inspirecloud.run 调用云函数
        inspirecloud
          .run(fnName, {
            userEmail: this.loginForm.email,
            userPassword: this.loginForm.password,
            userType: this.loginForm.type,
          })
          .then((data) => {
            // 处理正常结果
            console.log(data);
            // 注册成功
            if (data.token) {
              // 储存token
              window.sessionStorage.setItem("token", data.token);
              window.sessionStorage.setItem("userType", data.user.userType);
              window.sessionStorage.setItem("userId", data.user._id);
              // 消息提示
              this.$message.success("登录成功");
              this.$router.push("/home");
            } else {
              // 注册失败
              this.$message.error("注册失败，用户已存在");
            }
          })
          .catch((error) => {
            // 处理异常结果
          });
      });
    },

    // 登录验证，
    login() {
      this.$refs.loginFormRef.validate((valid) => {
        if (!valid) {
          return;
        }
        const InspireCloud = require("@byteinspire/js-sdk");
        const myServiceId = "qcqsjd";
        const fnName = "user_login";

        // 创建轻服务实例
        const inspirecloud = new InspireCloud({
          serviceId: myServiceId,
        });

        // 调用轻服务函数
        inspirecloud
          .run(fnName, {
            userEmail: this.loginForm.email,
            userPassword: this.loginForm.password,
            userType: this.loginForm.type,
          })
          .then((data) => {
            console.log(data);

            //成功登录
            if (data.token) {
              // 储存token
              window.sessionStorage.setItem("token", data.token);
              window.sessionStorage.setItem("userType", data.user.userType);
              window.sessionStorage.setItem("userId", data.user._id);
              // 消息提示
              this.$message.success("登录成功");
              this.$router.push("/home");
            } else {
              this.$message.error("账号或者密码错误");
            }
            // 登录失败
          })
          .catch((error) => {
            // 处理异常
          });
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 380px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px, solid, #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  margin: -65px auto;
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.activeTab {
  margin: 25px;
}
.login_form {
  position: absolute;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>

